package com.smartcar.websocket;

import com.smartcar.dto.CarControlRequest;
import com.smartcar.enums.CarDirection;
import com.smartcar.service.CarApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

/**
 * WebSocket消息处理器
 */
@Slf4j
@Controller
public class WebSocketHandler {

    private final CarApiService carApiService;

    public WebSocketHandler(CarApiService carApiService) {
        this.carApiService = carApiService;
    }

    /**
     * 处理按钮控制消息
     */
    @MessageMapping("/car/button")
    @SendTo("/topic/car/status")
    public String handleButtonControl(CarDirection direction) {
        try {
            carApiService.carBtnCtrl(direction);
            return "按钮控制成功: " + direction.name();
        } catch (Exception e) {
            log.error("按钮控制失败", e);
            return "按钮控制失败: " + e.getMessage();
        }
    }

    /**
     * 处理摇杆控制消息
     */
    @MessageMapping("/car/joystick")
    @SendTo("/topic/car/status")
    public String handleJoystickControl(CarControlRequest request) {
        try {
            if (request.getSpeedX() != null && request.getSpeedY() != null) {
                carApiService.ctrlCar(request.getSpeedX(), request.getSpeedY());
                return "摇杆控制成功: X=" + request.getSpeedX() + ", Y=" + request.getSpeedY();
            } else {
                return "摇杆控制失败: 速度参数不能为空";
            }
        } catch (Exception e) {
            log.error("摇杆控制失败", e);
            return "摇杆控制失败: " + e.getMessage();
        }
    }

    /**
     * 处理四轮控制消息
     */
    @MessageMapping("/car/wheels")
    @SendTo("/topic/car/status")
    public String handleWheelsControl(CarControlRequest request) {
        try {
            if (request.getL1() != null && request.getL2() != null && 
                request.getR1() != null && request.getR2() != null) {
                carApiService.upSpeedCar(request.getL1(), request.getL2(), request.getR1(), request.getR2());
                return "四轮控制成功: L1=" + request.getL1() + ", L2=" + request.getL2() + 
                       ", R1=" + request.getR1() + ", R2=" + request.getR2();
            } else {
                return "四轮控制失败: 轮子速度参数不能为空";
            }
        } catch (Exception e) {
            log.error("四轮控制失败", e);
            return "四轮控制失败: " + e.getMessage();
        }
    }

    /**
     * 处理循迹控制消息
     */
    @MessageMapping("/car/tracking")
    @SendTo("/topic/car/status")
    public String handleTrackingControl(boolean start) {
        try {
            if (start) {
                carApiService.startTracking();
                return "开始循迹成功";
            } else {
                carApiService.stopTracking();
                return "停止循迹成功";
            }
        } catch (Exception e) {
            log.error("循迹控制失败", e);
            return "循迹控制失败: " + e.getMessage();
        }
    }

    /**
     * 处理相机控制消息
     */
    @MessageMapping("/car/camera")
    @SendTo("/topic/car/status")
    public String handleCameraControl(String action) {
        try {
            switch (action.toLowerCase()) {
                case "photo":
                    carApiService.takePhoto();
                    return "拍摄图片成功";
                case "record_start":
                    carApiService.startRecording();
                    return "开始录像成功";
                case "record_stop":
                    carApiService.stopRecording();
                    return "停止录像成功";
                default:
                    return "未知的相机操作: " + action;
            }
        } catch (Exception e) {
            log.error("相机控制失败", e);
            return "相机控制失败: " + e.getMessage();
        }
    }
} 